package com.ruoyi.common.rabbitmq.consumer;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.oms.dao.YytOmsOrderMapper;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 监听死信队列
 */
@Component
public class OrderStatusService {

    @Autowired
    private YytOmsOrderMapper yytOmsOrderMapper;

    @Autowired
    private YytSmsCouponHistoryService yytSmsCouponHistoryService;

    @RabbitListener(queues = "yuanju.dead.order.queue")
    public void OrderMessage(String orderId){
        YytOmsOrder yytOmsOrder = yytOmsOrderMapper.selectById(orderId);
        if(yytOmsOrder != null){
            if(yytOmsOrder.getStatus() == 0){
                UpdateWrapper<YytOmsOrder> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", orderId).set("status", 4);
                yytOmsOrderMapper.update(yytOmsOrder, wrapper);
                // 如果使用了优惠券，则把优惠券重置
                if(StringUtils.isNotBlank(yytOmsOrder.getCouponId())){
                    yytSmsCouponHistoryService.upCouponModeStatus(yytOmsOrder.getCouponId(), yytOmsOrder.getId());
                }
                System.out.println("订单号为："+ orderId + "  已超过30分钟，订单已取消");
            }
        }
    }
}
